package com.cin.cma.server.pos.service.impl;

import com.cin.cma.common.enums.JyServerErrorCode;
import com.cin.cma.common.exception.UserFriendlyException;
import com.cin.cma.common.model.finixx.base.BaseRequest;
import com.cin.cma.common.model.finixx.base.BaseResponse;
import com.cin.cma.common.model.finixx.request.*;
import com.cin.cma.common.model.finixx.response.*;
import com.cin.cma.common.constant.FunctionCode;
import com.cin.cma.server.pos.service.ICardService;
import com.cin.cma.server.pos.service.IProxyRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @Date：2024-07-18 14:42
 */
@Slf4j
@Service
public class CardServiceImpl implements ICardService {
    @Autowired
    private IProxyRequestService proxyRequestService;

    /**
     * 会员卡消费
     *
     * @param model
     * @return
     */
    @Override
    public CardConsumeResponse consume(CONCardConsumeRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_CONSUME);
        return this.proxyRequestService.context(model, CardConsumeResponse.class);
    }

    /**
     * 获取会员卡信息
     *
     * @param model
     * @return
     */
    @Override
    public CardInfoResponse getCardInfo(CardInfoRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_INFORMATION);
        return this.proxyRequestService.context(model, CardInfoResponse.class, result -> {
            //3003    查无此卡
            //3004    查无此使用政策
            //3005    查无此等级政策
            switch (result) {
                case 3003:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_NOT_FOUND);
                case 3004:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_USE_POLICY_NOT_FOUND);
                case 3005:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_GRADE_NOT_FOUND);
                default:
                    break;
            }
        });
    }

    /**
     * 会员卡购票消费回滚
     *
     * @param model
     * @return
     */
    @Override
    public BaseResponse consumeRollback(ConsumeRollbackRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_CONSUME_ROLLBACK);
        return this.proxyRequestService.context(model, BaseResponse.class);
    }

    /**
     * 获取会员卡摘要信息
     *
     * @param model
     * @return
     */
    @Override
    public SummaryInfoResponse summaryInfo(CardSummaryInfoRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_GET_GETSUMMARYINFO);
        return this.proxyRequestService.context(model, SummaryInfoResponse.class);
    }

    /**
     * 卡充值
     *
     * @param model
     * @return
     */
    @Override
    public CardAddMoneyResponse addMoney(CardAddMoneyRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_ADD_MONEY);
        return this.proxyRequestService.context(model, CardAddMoneyResponse.class, result -> {
            switch (result) {
                //3137    充值金额太多，需小于99999
                //3042    找不到会员卡信息
                //3043    卡状态不符
                //3057    卡类型不允许充值
                //3088    非最少充值额倍数
                //2024    取充值流水号出错
                //3045    没有插入任何数据
                //10109   第三方交易未完成
                case 3137:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_MAX_AMOUNT);
                case 3042:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_NOT_FOUND);
                case 3043:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_STATE_ERROR);
                case 3057:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_NOT_SUPPER_RECHARGE);
                case 3088:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_NOT_RECHARGE_MULTIPLE);
                case 2024:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_GETORDERCODE_ERROR);
                case 3045:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_INSERT_ERROR);
                case 10109:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_THIRD_PAYMENT_FAILED);
                default:
                    break;
            }
        });
    }

    /**
     * 查询卡消费记录
     *
     * @param model
     * @return
     */
    @Override
    public CardQueryBuyingResponse buying(CardBuyingRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_BUYING);
        return this.proxyRequestService.context(model, CardQueryBuyingResponse.class);
    }

    /**
     * 查询卡充值记录
     *
     * @param model
     * @return
     */
    @Override
    public CardQueryAddMoneyResponse getAddMoney(CardMarkingRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_GET_ADDMONEY);
        return this.proxyRequestService.context(model, CardQueryAddMoneyResponse.class);
    }

    /**
     * 查询卡积分记录
     *
     * @param model
     * @return
     */
    @Override
    public CardQueryMarkingResponse getMarking(CardMarkingRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_GET_MARKING);
        return this.proxyRequestService.context(model, CardQueryMarkingResponse.class);
    }

    /**
     * 修改会员卡密码
     *
     * @param model
     * @return
     */
    @Override
    public BaseResponse resetPwd(CardUpdatePwdRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_RESETPASSWORD);
        return this.proxyRequestService.context(model, BaseResponse.class);
    }

    /**
     * 会员卡锁卡
     *
     * @param model
     * @return
     */
    @Override
    public BaseResponse lock(CardLockRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_LOCK);
        return this.proxyRequestService.context(model, BaseResponse.class);
    }

    /**
     * 会员卡激活
     *
     * @param model
     * @return
     */
    @Override
    public BaseResponse activation(CardActivationRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_ACTIVATION);
        return this.proxyRequestService.context(model, BaseResponse.class);
    }

    /**
     * 修改会员卡信息
     *
     * @param model
     * @return
     */
    @Override
    public BaseResponse editMemberInfo(CardEditMemberInfoRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_EDITMEMBERINFO);
        return this.proxyRequestService.context(model, BaseResponse.class);
    }

    /**
     * 会员卡发卡（卡务必已制卡状态）
     *
     * @param model
     * @return
     */
    @Override
    public CardSellResponse sell(CardSellRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_SELL);
        return this.proxyRequestService.context(model, CardSellResponse.class);
    }

    /**
     * 获取卡信息（发卡前）
     *
     * @param model
     * @return
     */
    @Override
    public CardSellInfoResponse sellInfo(CardSellInfoRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_SELL_INFO);
        return this.proxyRequestService.context(model, CardSellInfoResponse.class);
    }

    /**
     * 会员卡发卡（卡务必已制卡状态）
     *
     * @param model
     * @return
     * @throws Exception
     */
    @Override
    public CardSellRechargeResponse sellRecharge(CardSellRechargeRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_SELL_RECHARGE);
        return this.proxyRequestService.context(model, CardSellRechargeResponse.class, result -> {
            switch (result) {
                case 3025:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_ISSUED);
                case 3088:
                    throw new UserFriendlyException(JyServerErrorCode.MEMBER_CARD_NOT_RECHARGE_MULTIPLE);
            }
        });
    }

    /**
     * 获取会员卡使用政策列表
     *
     * @param model
     * @return
     */
    @Override
    public CardPrefabricateUsePolicyResponse getUsePolicyList(BaseRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_GET_USE_POLICY_LIST);
        return this.proxyRequestService.context(model, CardPrefabricateUsePolicyResponse.class);
    }

    /**
     * 获取会员卡使用政策信息
     *
     * @param model
     * @return
     */
    @Override
    public CardUsePolicyResponse usePolicy(CardUsePolicyRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_USE_POLICY);
        return this.proxyRequestService.context(model, CardUsePolicyResponse.class);
    }

    /**
     * 获取会员卡等级政策信息
     *
     * @param model
     * @return
     */
    @Override
    public CardPrefabricateGradeResponse getGradeList(BaseRequest model) throws Exception {
        model.setCode(FunctionCode.CARD_GET_GRADE_POLICY_LIST);
        return this.proxyRequestService.context(model, CardPrefabricateGradeResponse.class);
    }
}
